package pl.edu.wsinf.lukaszwalczak.emailclient.view;

/**
 *
 * @author Łukasz
 */
import java.util.HashMap;
import java.util.Map;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javax.inject.Inject;
import org.springframework.stereotype.Component;
import pl.edu.wsinf.lukaszwalczak.emailclient.controller.ContactCreateController;

@Component
public class ContactCreateView {

    @Inject
    private ContactCreateController controller;
    private Stage stage = null;

    public void show() {
        if (stage == null) {
            stage = new Stage();
            //resizable disabled
            stage.setResizable(false);
            //create root node of scene, i.e. group
            AnchorPane root = new AnchorPane();
            //create scene with set width, height and color
            Scene scene = new Scene(root, 460, 300, Color.WHITESMOKE);
            //set scene to stage
            stage.setScene(scene);
            stage.setTitle("Dodaj kontakt");
            //center stage on screen
            stage.centerOnScreen();
            // stage icon
            Image contactsIcon = new Image(MainWindowView.class.getResourceAsStream("/icons/address-book-add-16.png"));
            stage.getIcons().add(contactsIcon);
            //show the stage
            stage.show();

            int LEFT_PADDING = 20;
            int RIGHT_PADDING = 20;
            int TOP_PADDING = 15;
            int BOTTOM_PADDING = 15;
            int FIELD_SPACING = 25;
            int LABEL_WIDTH = 190;
            int TEXT_FIELD_WIDTH = 250;


            // Pole "imię"
            Label nameLabel = new Label("Imię:");
            nameLabel.setPrefWidth(LABEL_WIDTH);
            final TextField nameField = new TextField();
            nameField.setPrefWidth(TEXT_FIELD_WIDTH);

            //Pole "nazwisko"             
            Label surnameLabel = new Label("Nazwisko:");
            surnameLabel.setPrefWidth(LABEL_WIDTH);
            final TextField surnameField = new TextField();
            surnameField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "wyświetlanie"
            Label displayLabel = new Label("Zapisz jako:");
            displayLabel.setPrefWidth(LABEL_WIDTH);
            final TextField displayField = new TextField();
            displayField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "adres email"
            Label emailLabel = new Label("Adres email: ");
            emailLabel.setPrefWidth(LABEL_WIDTH);
            final TextField emailField = new TextField();
            emailField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "nr telefonu"
            Label phoneLabel = new Label("Telefon: ");
            phoneLabel.setPrefWidth(LABEL_WIDTH);
            final TextField phoneField = new TextField();
            phoneField.setPrefWidth(TEXT_FIELD_WIDTH);

            // Pole "adres"
            Label addressLabel = new Label("Adres:");
            addressLabel.setPrefWidth(LABEL_WIDTH);
            final TextArea addressField = new TextArea();
            addressField.setPrefWidth(TEXT_FIELD_WIDTH);
            addressField.setPrefHeight(100);

            // Przycisk "zapis"
            Button btn1 = ViewHelper.createButton("Zapisz", new EventHandler<ActionEvent>() {
                @Override
                public void handle(ActionEvent event) {
                    Map<String, String> values = new HashMap<>();
                    values.put("name", nameField.getText());
                    values.put("surname", surnameField.getText());
                    values.put("label", displayField.getText());
                    values.put("email", emailField.getText());
                    values.put("phone", phoneField.getText());
                    values.put("address", addressField.getText());
                    controller.createContact(values);
                    stage.close();
                }
            }, "save.png", 200d, 32d);

            root.getChildren().addAll(nameLabel, nameField, surnameLabel, surnameField,
                    displayLabel, displayField, emailLabel, emailField, phoneLabel, phoneField,
                    addressLabel, addressField, btn1);

            AnchorPane.setTopAnchor(nameLabel, Double.valueOf(TOP_PADDING));
            AnchorPane.setLeftAnchor(nameLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(nameField, Double.valueOf(TOP_PADDING));
            AnchorPane.setLeftAnchor(nameField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(surnameLabel, Double.valueOf(TOP_PADDING + 1 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(surnameLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(surnameField, Double.valueOf(TOP_PADDING + 1 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(surnameField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(displayLabel, Double.valueOf(TOP_PADDING + 2 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(displayLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(displayField, Double.valueOf(TOP_PADDING + 2 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(displayField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(emailLabel, Double.valueOf(TOP_PADDING + 3 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(emailLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(emailField, Double.valueOf(TOP_PADDING + 3 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(emailField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(phoneLabel, Double.valueOf(TOP_PADDING + 4 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(phoneLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(phoneField, Double.valueOf(TOP_PADDING + 4 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(phoneField, Double.valueOf(LABEL_WIDTH));
            AnchorPane.setTopAnchor(addressLabel, Double.valueOf(TOP_PADDING + 5 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(addressLabel, Double.valueOf(LEFT_PADDING));
            AnchorPane.setTopAnchor(addressField, Double.valueOf(TOP_PADDING + 5 * FIELD_SPACING));
            AnchorPane.setLeftAnchor(addressField, Double.valueOf(LABEL_WIDTH));

            AnchorPane.setBottomAnchor(btn1, Double.valueOf(BOTTOM_PADDING));
            AnchorPane.setRightAnchor(btn1, Double.valueOf(RIGHT_PADDING));

        } else {
            stage.show();
        }
    }
}
